﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Light.Framework
{
    /// <summary>
    /// 查询结果列(为null或不指定的时候，查询所有)
    /// </summary>
    [Serializable]
    public class ResultColumn
    {

        private List<string> includes = null;
        private List<string> excludes = null;

        public ResultColumn()
        {
            includes = new List<string>();
            excludes = new List<string>();
        }

        /// <summary>
        /// 添加需要查询的属性名
        /// </summary>
        /// <param name="propertyName"></param>
        public void Add(string propertyName)
        {
            if (!includes.Contains(propertyName) && !String.IsNullOrWhiteSpace(propertyName))
                includes.Add(propertyName);
        }

        /// <summary>
        /// 删除不需要查询的属性名
        /// </summary>
        /// <param name="propertyName"></param>
        public void Delete(string propertyName)
        {
            if (!excludes.Contains(propertyName))
                excludes.Add(propertyName);
        }

        /// <summary>
        /// 结果中是否包含指定的属性名
        /// </summary>
        /// <param name="propertyName"></param>
        /// <returns></returns>
        public bool Include(string propertyName)
        {
            if (includes.Count > 0 && !String.IsNullOrWhiteSpace(propertyName) && !includes.Exists(t => t.StartsWith(propertyName)))
            {
                return false;
            }
            if (!String.IsNullOrWhiteSpace(propertyName) && excludes.Exists(t => propertyName.StartsWith(t)))
            {
                return false;
            }
            return true;
        }

        /// <summary>
        /// 是否为空（未设置查询结果项，表示查询全部）
        /// </summary>
        public bool IsEmpty
        {
            get
            {
                return (includes == null || includes.Count == 0)
                    && (excludes == null || excludes.Count == 0);
            }
        }

        public override string ToString()
        {
            StringBuilder builder = new StringBuilder();
            if (includes.Count > 0)
            {
                foreach (var c in includes)
                {
                    builder.Append(c + ",");
                }
                builder.Remove(builder.Length - 1, 1);
            }
            else
            {
                builder.Append(" * ");
            }
            return builder.ToString();
        }
    }
}
